package com.tronsis.distribution.activity.me.points;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.RulesMapBean;
import com.tronsis.distribution.biz.factory.CommonFactory;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.util.CommonUtil;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawalActivity extends ToolBarBaseActivity {


    @BindView(R.id.et_points)
    EditText mEtPoints;
    @BindView(R.id.ll_my_stock)
    LinearLayout mLlMyStock;
    @BindView(R.id.tv_amounts)
    TextView mTvAmounts;
    @BindView(R.id.ll_my_delivery)
    LinearLayout mLlMyDelivery;
    @BindView(R.id.btn_confirm_withdraw)
    Button mBtnConfirmWithdraw;
    @BindView(R.id.tv_rule)
    TextView mTvRule;

    private double mPointCovert = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawal);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.withdrawal));

        RulesMapBean rulesMapBean = CommonUtil.getRulesMap(this);
        if (rulesMapBean != null && rulesMapBean.getRULE_UPGRADE() != null) {
            mTvRule.setText("提现规则：\n"+rulesMapBean.getRULE_WITHDRAW());
        }

        mEtPoints.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mTvAmounts.setText(getString(R.string.format_amount_with_unit, mEtPoints.getText()));
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        httpGetPointConvert();
    }

    @OnClick(R.id.btn_confirm_withdraw)
    void confirmWithdrawClick() {
        String point = mEtPoints.getText().toString();
        int applyPoint = Integer.parseInt(point);
        if (StringUtil.isBlank(mEtPoints.getText().toString())) {
            ToastUtil.showMessage(this, "请输入提现积分");
            return;
        }
        int points = UserBizFactory.createBiz().getLoginUser(this).getPoints();
        if(points<applyPoint){
            ToastUtil.showMessage(this, "申请提现积分不够");
            return;
        }
        httpPointWithDrawApply(point);
    }

    private void httpPointWithDrawApply(String point) {
        try {
            UserBizFactory.createBiz().applyPointWithDraw(this, point, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    setResult(RESULT_OK);
                    finish();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });

        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void httpGetPointConvert() {
        try {
            CommonFactory.createBiz().pointConvert(this, new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    mPointCovert = (double) data;
                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
